// 提供用户相关的接口
const express = require('express');
// 创建路由
const router = express.Router();
// 引入db
const pool = require('../db');
// 引入md5加密
const utility = require('utility');

// 获取用户信息
router.get('/userinfo', (req, res) => {
    // 查询语句     req.user.username可以获取token
    pool.query('select * from user where username = ?', req.user.username, (err, results) => {
        // 查到数据量就是大于0
        console.log(results[0]);
        if (results.length > 0) {
            res.send({
                status: 0,
                message: '获取用户信息成功',
                data: results[0]
            })
        } else {
            res.send({
                status: 1,
                message: '获取用户信息失败'
            })
        }
    })
})

// 更新用户信息
router.post('/userinfo', (req, res) => {
    // 查询语句
    const {
        nickname,
        email,
        id
    } = req.body
    pool.query('update user set nickname = ? , email = ? where id = ?', [nickname, email, id], (err, results) => {
        // 查到数据量就是大于0
        console.log(results);
        if (results.affectedRows > 0) {
            res.send({
                status: 0,
                message: '更新用户信息成功',
            })
        } else {
            res.send({
                status: 1,
                message: '更新用户信息失败'
            })
        }
    })
})
// 重置密码
router.post('/updatepwd', (req, res) => {
    // 获取post请求体的内容
    let {
        oldPwd,
        newPwd
    } = req.body;
    // 给新旧密码加盐加密
    oldPwd = utility.md5('abc' + oldPwd + 'efg');
    newPwd = utility.md5('abc' + newPwd + 'efg');
    // req.user可以拿到token
    const {
        username
    } = req.user;
    // 通过保存的在token中的username查找用户 并判断它的密码是否是传过来的旧密码，如果不是旧密码就是错误
    pool.query('select * from user where username = ? and password = ?', [username, oldPwd], (err, results) => {
        // 结果数量等于0 就是没查到
        if (results.length === 0) {
            return res.send({
                status: 1,
                message: '旧密码错误',
            })
        }
        // 旧密码相同
        pool.query('update user set password = ? where username = ?', [newPwd, username], (err, results) => {
            if (results.affectedRows === 1) {
                res.send({
                    status: 0,
                    message: "更新密码成功！"
                })
            } else {
                res.send({
                    status: 1,
                    message: "更新密码失败！"
                })
            }
        })
    })
})

// 更换头像
router.post('/update/avatar', (req, res) => {
    let {
        avatar
    } = req.body;
    const {
        username
    } = req.user;
    pool.query('update user set user_pic = ? where username = ?', [avatar, username], (err, results) => {
        console.log(results);
        if (results.affectedRows > 0) {
            res.send({
                status: 0,
                message: "更新头像成功！"
            })
        }else{
            res.send({
                status: 1,
                message: "更新头像失败！"
            })
        }
    })
})





module.exports = router